import ServiceRequest from './request'
import { BASE_URL, TIMEOUT } from './config'
import { localCache } from '@/utils/cache'

const service = new ServiceRequest({
  baseURL: BASE_URL,
  timeout: TIMEOUT,
  interceptors: {
    requestSuccessFn: (config) => {
      const token = localCache.getCache('token')
        if(config.headers && token) {
          config.headers.Authorization = `Bearer ${token}`
        }
      return config
    }
  }
})

// 多个实例
export const service2 = new ServiceRequest({
  baseURL: 'http://codercba.com:1888/airbnb/api',
  timeout: 60000,
  interceptors: {
    requestSuccessFn: (config) => {
      console.log('单个实例的请求成功的拦截')
      return config
    },
    requestFailureFn: (err) => {
      return err
    },
    responseSuccessFn: (res) => {
      console.log('单个实例的响应成功的拦截')
      return res
    },
    responseFailureFn: (err) => {
      return err
    }
  }
})

export default service
